<?php

namespace app\api\controller;

use app\admin\model\Memberinvoice as MemberinvoiceMdl;

use think\Db;
use think\Exception;

class Invoice extends Base
{

    /**
     * 获取发票抬头列表
     */
    public function getList()
    {
        $param = self::getHttpParam();

        if (empty($param->memberId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '会员ID(memberId) 不能为空');
        }

        $where = [
            'member_id' => $param->memberId,
        ];

        $model = new MemberinvoiceMdl();
        $fields = 'id, company, tax_number';
        $order = 'create_date DESC';
        $list = $model->where($where)->field($fields)->order($order)->select();
        if(!$list){
            return show(config('status.ERROR_STATUS'), '没有发票抬头', '没有发票抬头');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list);
    }

    /**
     * 保存
     */
    public function save()
    {
        $param = self::getHttpParam();

        if (empty($param->memberId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '会员ID(memberId) 不能为空');
        }
        if (empty($param->company)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '公司名称(company) 不能为空');
        }
        if (empty($param->tax_number)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '纳税人识别号(tax_number) 不能为空');
        }

        $data = [
            'member_id' => $param->memberId,
            'company' => $param->company,
            'tax_number' => $param->tax_number
        ];

        Db::startTrans();
        try {
            $model = new MemberinvoiceMdl();

            if (!empty($param->id)) {
                // 修改
                $data['id'] = $param->id;
                $data['update_date'] = date('Y-m-d H:i:s');

                $result = $model->allowField(true)->save($data, ['id' => $data['id']]);
            } else {
                // 新增
                $data['create_date'] = date('Y-m-d H:i:s');

                $result = $model->save($data);
            }

            if($result !== false){
                Db::commit();
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '保存成功');
            }else{
                Db::rollback();
                return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $e->getMessage());
        }
    }

    /**
     * 删除
     */
    public function delete(){
        $param = self::getHttpParam();

        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '发票抬头(id) 不能为空');
        }

        $where = [
            'id' => $param->id
        ];
        $model = new MemberinvoiceMdl();
        $result = $model->where($where)->delete();

        if($result === false){
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, '删除失败');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '删除成功');
    }
}